package com.ltxtiyu.video.controller;


import com.alibaba.dubbo.config.annotation.Reference;

import com.ltxtiyu.video.common.JsonTool;
import com.ltxtiyu.video.common.RetEnum;
import com.ltxtiyu.video.entity.Manager;
import com.ltxtiyu.video.service.ms.ManagerService;
import com.ltxtiyu.video.tool.SecurityUtil;
import com.ltxtiyu.video.tool.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("managerController")
@Controller
public class ManagerController {

    @Reference
   private ManagerService ms;

    /**
     * 条件查询列表
     * @param page
     * @param limit
     * @param name
     * @return
     */
    @RequestMapping("paramlist")
    @ResponseBody
    public Map<String,Object> paramList( int page,int limit, String name){
        Map<String,Object> map = new HashMap<>();
        map.put("page",(page-1)*limit);
        map.put("row",limit);
        if(StringUtil.isNotBlank(name)){

            map.put("name",name);
        }
        Map<String,Object> result = ms.selectParamList(map);
        return result;
    }




    /**
     * 新增与更新
     * @param manager
     * @param id
     * @return
     */
    @RequestMapping("addAndUpdate")
    @ResponseBody
    public  String  addMenu(Manager manager, Integer id){
        if( id != null){
            //更新
            return ms.updateManager(manager,id);
        }else{
            //新增
            return ms.insertManager(manager);
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("delByIds")
    @ResponseBody
    public  String delByIds(String ids){
        return ms.delByIds(ids);
    }



    /**
     * 管理员详情
     * @param id
     * @return
     */
    @RequestMapping("detail")
    @ResponseBody
    @PostMapping
    public  Map<String, Object> detail(Integer id){
        return ms.detail(id);
    }

    /**
     * 当前登录用户
     * @param request
     * @return
     */
    @RequestMapping("getAdmin")
    @ResponseBody
    public  Map<String, Object> getAdmin(HttpServletRequest request){
        Manager loginUser = (Manager) request.getSession().getAttribute("loginUser");
        Map<String, Object> detail = ms.detail(loginUser.getId());
       return detail;

    }

    /**
     * 当前用户信息修改
     * @param manager
     * @return
     */
    @RequestMapping("changeInfo")
    @ResponseBody
    public String changeInfo(HttpServletRequest request,Manager manager){
        Manager loginUser = (Manager) request.getSession().getAttribute("loginUser");
        manager.setId(loginUser.getId());
        return ms.changeInfo(manager);
    }

    /**
     *当前用户密码修改
     * @param request
     * @param newPassword
     * @return
     */
    @RequestMapping("changePwd")
    @ResponseBody
    public String changePwd(HttpServletRequest request,String newPassword,String oldPassword){
        Manager loginUser = (Manager) request.getSession().getAttribute("loginUser");
        if(!loginUser.getPassword().equals(SecurityUtil.encrypt(oldPassword))){
            return JsonTool.genJson(RetEnum.NEW_PASSWORD_IS_ERROR);
        }
        loginUser.setPassword(SecurityUtil.encrypt(newPassword));
        return ms.changePwd(loginUser);
    }

    /**
     * 管理员密码修改
     * @param id
     * @param newPassword
     * @return
     */
    @RequestMapping("updatePassword")
    @ResponseBody
    public  String updatePwd(Integer id,String newPassword){
            return ms.updatePwd(id,newPassword);
    }
}
